{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Linklist Add Up\n",
    "\n",
    "[The Original Question](https://mp.weixin.qq.com/s/EKTQUaTGu2_0vRioEtIAjw)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Question\n",
    "\n",
    "You are given two linked-lists representing two non-negative integers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "7 -> 0 -> 0 -> 1\n"
     ]
    }
   ],
   "source": [
    "class Node:\n",
    "    def __init__(self, value, next=None):\n",
    "        self.value = value\n",
    "        self.next = next\n",
    "        pass\n",
    "    \n",
    "    def __str__(self):\n",
    "        array = list()\n",
    "        while self is not None:\n",
    "            array.append(str(self.value))\n",
    "            self = self.next\n",
    "            pass\n",
    "        return \" -> \".join(array)\n",
    "    \n",
    "    pass\n",
    "\n",
    "\n",
    "def add_up(array1, array2):\n",
    "    sum = array1.value + array2.value\n",
    "    pointer = Node(sum % 10)\n",
    "    result = pointer\n",
    "    flag = sum // 10\n",
    "    array1 = array1.next\n",
    "    array2 = array2.next\n",
    "    while array1 is not None and array2 is not None:\n",
    "        sum = array1.value + array2.value + flag\n",
    "        pointer.next = Node(sum % 10)\n",
    "        flag = sum // 10\n",
    "        array1 = array1.next\n",
    "        array2 = array2.next\n",
    "        pointer = pointer.next\n",
    "        pass\n",
    "    while array1 is not None:\n",
    "        sum = array1.value + flag\n",
    "        pointer.next = Node(sum % 10)\n",
    "        flag = sum // 10\n",
    "        array1 = array1.next\n",
    "        pointer = pointer.next\n",
    "        pass\n",
    "    while array2 is not None:\n",
    "        sum = array2.value + flag\n",
    "        pointer.next = Node(sum % 10)\n",
    "        flag = sum // 10\n",
    "        array2 = array2.next\n",
    "        pointer = pointer.next\n",
    "        pass\n",
    "    if flag != 0:\n",
    "        pointer.next = Node(flag)\n",
    "        pass\n",
    "    return result\n",
    "\n",
    "\n",
    "if __name__ == \"__main__\":\n",
    "    arr1 = Node(2, Node(4, Node(5)))\n",
    "    arr2 = Node(5, Node(6, Node(4)))\n",
    "    res = add_up(arr1, arr2)\n",
    "    print(res)\n",
    "    pass"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
